import { loginAPI } from '@/api'
import { getToken, removeToken, setToken } from '@/utils/auth'

// 用户模块校验登录
export default {
  state: {
    token: getToken() // 获取本地缓存的token
  },
  mutations: {
    setToken(state, token) { // 执行函数将token储存到本地
      state.token = token
      setToken(token)
    },
    removeToken(state) { // 删除本地缓存token
      state.token = null
      removeToken()
    }
  },
  actions: {
    async login(store, data) {
      store.commit('setToken', await loginAPI(data))
    }
  },
  namespaced: true
}
